package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("getUsername")
    public Result getusername(){
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user == null){
                return new Result(false,MessageConstant.GET_USERNAME_FAIL);
            }
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,"删除用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
}
